/**
 * 
 */
package fr.cephb.joperon.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import fr.cephb.util.Cast;

/**
 * @author lindenb
 *
 */
public class SimpleRegionRenderer extends DefaultTableCellRenderer
	{
	private static final long serialVersionUID = 1L;
	private long chromMin=0;
	private long chromMax=0;
	private long chromStart=0;
	private long chromEnd=0;
	private int colStart=-1;
	private int colEnd=-1;
	
	public SimpleRegionRenderer(int colStart,int colEnd)
		{
		setOpaque(true);
		setBackground(Color.WHITE);
		this.colStart=colStart;
		this.colEnd= colEnd;
		}
	
	public void setChromMin(long chromMin)
		{
		this.chromMin= chromMin;
		}
	
	public void setChromMax(long chromMax) {
		this.chromMax = chromMax;
		}
	
	@Override
	protected void paintComponent(Graphics g1) {
		super.paintComponent(g1);
		Graphics2D g= Graphics2D.class.cast(g1);
		LinearGradientPaint paint= new LinearGradientPaint(
			this.getWidth()/2,0,
			this.getWidth()/2,this.getHeight(),
			new float[]{0f,0.5f,1f},
			new Color[]{Color.GRAY,Color.WHITE,Color.GRAY}
			);
		Paint old= g.getPaint();
		g.setPaint(paint);
		double w2= getWidth()*0.7;
		double h2= getHeight()*0.7;
		double x1= (this.getWidth()-w2)/2+ w2*((Math.max(this.chromMin,this.chromStart)-this.chromMin)/(double)(this.chromMax-this.chromMin));
		double x2= (this.getWidth()-w2)/2+ w2*((Math.min(this.chromMax,this.chromEnd)-this.chromMin)/(double)(this.chromMax-this.chromMin));
		Rectangle2D r= new Rectangle2D.Double(
			x1,
			(this.getHeight()-h2)/2,
			(x2-x1),
			h2);
		g.fill(r);
		g.setPaint(old);
		g.setColor(Color.BLACK);
		g.draw(r);
		}
		
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		this.chromStart= Cast.Integer.cast(table.getModel().getValueAt(row, this.colStart).toString());
		this.chromEnd= Cast.Integer.cast(table.getModel().getValueAt(row, this.colEnd).toString());
		return this;
		}
}
